package pers.archives.loginToken.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import pers.archives.common.constant.SecurityConstant;
import pers.archives.loginToken.authentication.ArchivesAuthenticationFailureHandler;
import pers.archives.loginToken.authentication.ArchivesAuthenticationSuccessHandler;

/* 登录以及登出配置
 *@program:archives-parent
 *@author: 赵浩浩
 *@Time: 2020/9/26  10:27
 */
@Component
public class LoginAuthenticationConfig {
    @Autowired(required = false)
    private ArchivesAuthenticationSuccessHandler archivesAuthenticationSuccessHandler;

    @Autowired(required = false)
    private ArchivesAuthenticationFailureHandler archivesAuthenticationFailureHandler;

    @Autowired
    private LogoutSuccessHandler archivesLogoutSuccessHandler;

    public void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage(SecurityConstant.DEFAULT_AUTHENTICATION_URL)
                .loginProcessingUrl(SecurityConstant.DEFAULT_SIGN_IN_PROCESSING_URL_FORM) //设置登陆时的路径
                .successHandler(archivesAuthenticationSuccessHandler) //登陆成功处理器
                .failureHandler(archivesAuthenticationFailureHandler) //登陆失败处理器
                .and()
                //登出配置
                .logout()
                .logoutUrl(SecurityConstant.DEFAULT_LOGOUT_URL)
                .logoutSuccessHandler(archivesLogoutSuccessHandler);
    }
}
